package com.ruoyi.miniapp.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.miniapp.api.domain.MiniappDictType;
import com.ruoyi.miniapp.service.IMiniappDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 字典类型Controller
 *
 * @author ruoyi
 * @date 2023-09-16
 */
@RestController
@RequestMapping("/type")
public class MiniappDictTypeController extends BaseController
{
    @Autowired
    private IMiniappDictTypeService miniappDictTypeService;

    /**
     * 查询字典类型列表
     */
    @RequiresPermissions("miniapp:type:list")
    @GetMapping("/list")
    public TableDataInfo list(MiniappDictType miniappDictType)
    {
        startPage();
        List<MiniappDictType> list = miniappDictTypeService.selectMiniappDictTypeList(miniappDictType);
        return getDataTable(list);
    }

    /**
     * 导出字典类型列表
     */
    @RequiresPermissions("miniapp:type:export")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiniappDictType miniappDictType)
    {
        List<MiniappDictType> list = miniappDictTypeService.selectMiniappDictTypeList(miniappDictType);
        ExcelUtil<MiniappDictType> util = new ExcelUtil<MiniappDictType>(MiniappDictType.class);
        util.exportExcel(response, list, "字典类型数据");
    }

    /**
     * 获取字典类型详细信息
     */
    @RequiresPermissions("miniapp:type:query")
    @GetMapping(value = "/{dictId}")
    public AjaxResult getInfo(@PathVariable("dictId") Long dictId)
    {
        return success(miniappDictTypeService.selectMiniappDictTypeByDictId(dictId));
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("miniapp:type:add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiniappDictType miniappDictType)
    {
        return toAjax(miniappDictTypeService.insertMiniappDictType(miniappDictType));
    }

    /**
     * 修改字典类型
     */
    @RequiresPermissions("miniapp:type:edit")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiniappDictType miniappDictType)
    {
        return toAjax(miniappDictTypeService.updateMiniappDictType(miniappDictType));
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("miniapp:type:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dictIds}")
    public AjaxResult remove(@PathVariable Long[] dictIds)
    {
        return toAjax(miniappDictTypeService.deleteMiniappDictTypeByDictIds(dictIds));
    }
}
